package zhongbo.java.air.airflightpath.service.Impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.airflightpath.listener.RedisLogChanelPublisher;
import zhongbo.java.air.airflightpath.mapper.AirFlightPathMapper;
import zhongbo.java.air.airflightpath.model.FlightPath;
import zhongbo.java.air.airflightpath.service.AirFlightPathService;
import zhongbo.java.air.common.vo.PageVo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class AirFlightPathServiceImpl implements AirFlightPathService {
    @Autowired
    private AirFlightPathMapper airFlightPathMapper;

    @Autowired
    private RedisLogChanelPublisher redisLogChanelPublisher;
    /*
      *icao_code必填字段，并且长度不超过6个字符(0字符虽然不超过6，但是内容无效)
      * 时间戳timestamp 必填，是否空字符串，符合ISO 8601格式精确到毫秒，空则无法解析为有效时间戳
      * 纬度和经度判断是否为0，纬度范围-90.000000 到90.000000，经度-180.000000到180.000000
      * 海拔高度altitude值>=0, 非负数
      * 地速 >=0  航向角0-359或者null
      * sourceType数据源类型（枚举值：ADS_B, RADAR, MLAT
      *
     */
    @Override
    public FlightPath add(FlightPath flightPath) {
        //关联飞机ICAO码不能为空
        if(StrUtil.isBlank(flightPath.getIcaoCode())){
            throw new RuntimeException("关联飞机ICAO码不能为空");
        }
        if(flightPath.getLatitude()==null || flightPath.getLongitude()==null){
            throw new RuntimeException("纬度和经度不能为空");
        }
        if(flightPath.getTimestamp()==null){
            throw new RuntimeException("时间戳不能为空");

        }
        //获得经度纬度,双经度转字符串
        String longitude =String.format("%.6f",flightPath.getLongitude());
        String latitude =String.format("%.6f",flightPath.getLatitude());

        String latitudedd ="^-?([1-8]?[1-9]|90)\\.\\d{6}$";
        String longituderr="^-?([1]?[1-7]?[1-9]|180)\\.\\d{6}$";

        Pattern pattern =Pattern.compile(latitudedd);
        Pattern pattern1=Pattern.compile(longituderr);

        Matcher matcher =pattern.matcher(latitude);
        Matcher matcher1 =pattern1.matcher(longitude);
        if(!matcher.matches() ||!matcher1.matches()){
            throw new IllegalArgumentException("纬度,经度范围不符合要求");
        }
         //添加时判断是否关联飞机ICAO码
        String  findICaoCode =airFlightPathMapper.selectCode(flightPath.getIcaoCode());
        //如果没有找到了
        if(StrUtil.isBlank(findICaoCode)){
            throw new RuntimeException("航迹表关联的飞机表icao_code不存在");
        }
        //执行添加
        int add=airFlightPathMapper.insert(flightPath);
        if(add>0){
            redisLogChanelPublisher.Push("新增航迹：ID=" +flightPath.getId());
            return flightPath;
        }return null;

    }


    @Override
    public String delete(String id) {
        //查id
        FlightPath select =airFlightPathMapper.selectById(id);
        if(select==null){
            throw new RuntimeException("该航迹信息不存在");
        }
        int delete =airFlightPathMapper.deleteById(id);
        if(delete>0){
            redisLogChanelPublisher.Push("正在删除的航迹：ID=" +id);
            return id;
        }
        return null;
    }

    @Override
    public FlightPath update(FlightPath flightPath) {
        //查id
        FlightPath select =airFlightPathMapper.selectById(flightPath.getId());
        if(select==null){
            throw new RuntimeException("该航迹信息不存在");
        }
        int update =airFlightPathMapper.updateById(flightPath);
        if (update>0){
            redisLogChanelPublisher.Push("更新修改的航迹信息：状态=" +flightPath.getIcaoCode());
            return flightPath;
        }
        return null;
    }

    @Override
    public FlightPath select(String id) {
        FlightPath select =airFlightPathMapper.selectById(id);
        if(select!=null){
            redisLogChanelPublisher.Push("查询到的航迹：=" +select);
            return select;
        }
        return null;
    }

    //分页查询
    @Override
    public PageVo<FlightPath> page(String id, Integer pageNumber, Integer pageSize) {
        QueryWrapper<FlightPath> queryWrapper =new QueryWrapper<>();//实体对象封装操作类
        if(StrUtil.isNotBlank(id)){//是否为空
            queryWrapper.like("id",id);
        }

        IPage<FlightPath> page =new Page<>(pageNumber,pageSize);
        IPage<FlightPath> airPAge =airFlightPathMapper.selectPage(page,queryWrapper);
        return new PageVo<>(airPAge);

    }
}
